package com.shu.collection;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2021/7/23 17:33
 */

public class CopyOnWriteArrayListTest {

    public static void main(String[] args) throws InterruptedException {
        int i1 = 2000;
        CountDownLatch countDownLatch = new CountDownLatch(i1);
        List copyOnWriteArrayList = new CopyOnWriteArrayList();
        List arrayList = new ArrayList();
        AtomicInteger idx = new AtomicInteger();
        for (int i = 0; i < i1; i++) {
            int get = idx.incrementAndGet();
            new Thread(()->{
                copyOnWriteArrayList.add(get);
                arrayList.add(get);
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
        System.out.println(copyOnWriteArrayList.toString());
        System.out.println(copyOnWriteArrayList.size());
        System.out.println("-----------");
        System.out.println(arrayList.toString());
        System.out.println(arrayList.size());
    }
}
